/*
 * create by straysnail
 * 2022/2/26
 */
#include <Adafruit_NeoPixel.h>
#define Neo_PIN  A0  //定义引脚
#define NUMPIXELS  11 //定义灯珠数量
//声明我们的NeoPixel strip对象
Adafruit_NeoPixel pixels(NUMPIXELS, Neo_PIN, NEO_GRB + NEO_KHZ800);

#define btnPin 2  //定义按钮引脚
#define btnPin2 13
boolean btnVal1;   //变量,用于接收按钮检测到的值
boolean btnVal2;
int count = 0; //变量,用于记录按钮按下的次数
boolean flag1 = 0; //用于切换按钮按下与松开的状态
boolean flag2 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(btnPin,INPUT);  //引脚11设置为输入状态
  pixels.begin(); // 初始化 NeoPixel
  pixels.clear(); //清除
  pixels.show();//显示
}

void loop() {
  btnVal1 = digitalRead(btnPin);  //读取按钮值，并赋给btnVal1
  while(btnVal1 == 0)  //判断,如果按钮被按下
  {
    delay(10);
    flag1 = 1;
    while(flag1 == 1) //按钮在被按下的状况下
    {
      btnVal1 = digitalRead(btnPin); //再次检测按钮的状态
      if(btnVal1 == 1)  //判断,如果按钮松开了
      {
        count = count + 1; //记录按钮被点击的次数
        Serial.println(count);  //打印出点击按钮的次数
        flag1 = 0; //退出按钮按下的状态
        if(count >= 6)
        {
          count = 6;
        }
        switch(count)
        {
          case 0: displayPiexls(NUMPIXELS, 200, 0, 0); pixels.show(); break;
          case 1: displayPiexls(NUMPIXELS, 0, 200, 0); pixels.show(); break;
          case 2: displayPiexls(NUMPIXELS, 0, 0, 200); pixels.show(); break;
          case 3: displayPiexls(NUMPIXELS, 200, 200, 0); pixels.show(); break;
          case 4: displayPiexls(NUMPIXELS, 0, 200, 200); pixels.show(); break;
          case 5: displayPiexls(NUMPIXELS, 200, 200, 200); pixels.show(); break;
          case 6: displayPiexls(NUMPIXELS, 0, 0, 0); pixels.show(); break;
        }
      }
    }
  }
  btnVal2 = digitalRead(btnPin2);  //读取按钮值，并赋给btnVal1
  while(btnVal2 == 0)  //判断,如果按钮被按下
  {
    delay(10);
    flag2 = 1;
    while(flag2 == 1) //按钮在被按下的状况下
    {
      btnVal2 = digitalRead(btnPin2); //再次检测按钮的状态
      if(btnVal2 == 1)  //判断,如果按钮松开了
      {
        count = count - 1; //记录按钮被点击的次数
        Serial.println(count);  //打印出点击按钮的次数
        flag2 = 0; //退出按钮按下的状态
        if(count <= 0)
        {
          count = 0;
        }
        switch(count)
        {
          case 0: displayPiexls(NUMPIXELS, 200, 0, 0); pixels.show(); break;
          case 1: displayPiexls(NUMPIXELS, 0, 200, 0); pixels.show(); break;
          case 2: displayPiexls(NUMPIXELS, 0, 0, 200); pixels.show(); break;
          case 3: displayPiexls(NUMPIXELS, 200, 200, 0); pixels.show(); break;
          case 4: displayPiexls(NUMPIXELS, 0, 200, 200); pixels.show(); break;
          case 5: displayPiexls(NUMPIXELS, 200, 200, 200); pixels.show(); break;
          case 6: displayPiexls(NUMPIXELS, 0, 0, 0); pixels.show(); break;
        }
      }
    }
  }
}

//定义一个函数，控制全部灯珠和其亮度
int displayPiexls(int num, int redVal, int greenVal, int blueVal)
{
  //使用for循环语句，让所有灯珠都亮起
  for(int i=0; i<num; i++) {
    pixels.setPixelColor(i, pixels.Color(redVal, greenVal, blueVal));//设置灯的引脚和RGB颜色
  }
}
